home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / UTILS / FILEUTIL.C < prev    next >
Text File  |  1992-12-02  |  13KB  |  602 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 030190    :    Update for Mac, DOS paths
  54.  *    SPK 012290    :    Initial
  55.  */
  56.  
  57. #include    "SystemPub.h"
  58. #include    "Proc.h"
  59. #include    "ShellPub.h"
  60. #include    "Path.h"
  61.  
  62. #include    "Prefs.h"
  63.  
  64. #define        BUFSIZE        512
  65.  
  66. Boolean        abortFileOp = FALSE;
  67.  
  68. #define        _DEBUG
  69.  
  70. /*******************************************************************/
  71.  
  72. char    *ExtractFile( char *path )
  73. {
  74. char    *cp, sep, *file;
  75.  
  76.     if( ShellPrefs.useMacOSPath )
  77.         sep = ':';
  78.     else if( ShellPrefs.useUNIXPath )
  79.         sep = '/';
  80.     else if( ShellPrefs.useDOSPath )
  81.         sep = '\\';
  82.         
  83.     cp = path;
  84.     file = cp;
  85.     
  86.     while( *cp )            /* find the file name in the path */
  87.         if( *cp == sep )
  88.             file = ++cp;
  89.         else
  90.             cp++;
  91.     
  92.     return( file );
  93. }
  94.  
  95. /*******************************************************************/
  96.  
  97. OSErr    ReadLine( int16 refNum, char *lineBuf, int32 bufSize )
  98. {
  99. int32    count;
  100. OSErr    err;
  101.  
  102.     while( bufSize-- )
  103.         {
  104.         count = 1L;
  105.         err = FSRead( refNum, &count, lineBuf );
  106.         
  107.         if( err || *lineBuf == '\n' || *lineBuf == '\r' )
  108.             {
  109.             *(++lineBuf) = '\0';
  110.             return( err );
  111.             }
  112.         else
  113.             lineBuf++;
  114.         }
  115.     return( noErr );
  116. }
  117.  
  118. /*******************************************************************/
  119.  
  120. int16        OpenFileDirect( char *filePath, int32 fileType, int16 perm )
  121. {
  122. int16        pwdVRefNum = 0, err,
  123.             fileRefNum = 0;
  124. char        buf[ BUFSIZE ], *cp;
  125. int32        parDirID, pwdDirID;
  126. pathType    pt;
  127. FInfo        finder;
  128.  
  129.     strcpy( buf, filePath );
  130.     pt = SetCurrPath( buf );
  131.  
  132.     if( pt == pathIsFile )
  133.         {
  134.         cp = GetLastScan();
  135.  
  136.         strcpy( buf, cp );
  137.         CtoPstr( buf );
  138.  
  139.         GetPWDInfo( &pwdVRefNum, &pwdDirID, &parDirID );
  140.         
  141.         err = HGetFInfo( pwdVRefNum, pwdDirID, buf, &finder );
  142.  
  143.         if( !err && (finder.fdType == fileType ))
  144.             err = HOpen( pwdVRefNum, pwdDirID, buf, perm, &fileRefNum );
  145.  
  146.         if( err )
  147.             fileRefNum = 0;
  148.         }
  149.         
  150.     return( fileRefNum );
  151. }
  152.  
  153. /*******************************************************************/
  154.  
  155. char    *ExtractPath( char *path, char *pbuf )
  156. {
  157.     while( *path != ';' && *path )
  158.         *pbuf++ = *path++;
  159.  
  160.     *pbuf = '\0';
  161.     
  162.     if( *path )
  163.         path++;
  164.     
  165.     return( path );
  166. }
  167.  
  168. /*******************************************************************/
  169.  
  170. int16        OpenFileInPath( char *path, char *filePath, int32 fileType,
  171.              int16 perm )
  172. {
  173. int16        pwdVRefNum = 0, err,
  174.             fileRefNum = 0;
  175. char        pathbuf[ BUFSIZE ], buf[ BUFSIZE ], *cp;
  176. int32        parDirID, pwdDirID;
  177. pathType    pt;
  178. FInfo        finder;
  179.  
  180. char        *nextPath;
  181. /*
  182.  *    For each path spec in the PATH var
  183.  *    try to open the file
  184.  */
  185.     GetPWDInfo( &pwdVRefNum, &pwdDirID, &parDirID );
  186.  
  187.     nextPath = path;
  188.     
  189.     while( *nextPath )
  190.         {
  191.         nextPath = ExtractPath( nextPath, pathbuf );
  192.         
  193.         /* if UNIX */
  194.         sprintf( buf, "%s/%s", pathbuf, filePath );
  195.  
  196.         fileRefNum = OpenFileDirect( buf, fileType, perm );
  197.         
  198.         if( fileRefNum )
  199.             {
  200.             strcpy( filePath, buf );
  201.             return( fileRefNum );
  202.             }
  203.         else
  204.             SetWD( pwdVRefNum, pwdDirID );
  205.         }
  206.         
  207.     return( fileRefNum );
  208. }
  209.  
  210. /*******************************************************************/
  211.  
  212. int16    OpenFile( WHandle ShellWh, char *filePath, int32 fileType,
  213.             int16 perm )
  214. {
  215. int16    fileRefNum = 0;
  216. char    *inPath;
  217.  
  218.     fileRefNum = OpenFileDirect( filePath, fileType, perm );
  219.     
  220.     if( fileRefNum )
  221.         return( fileRefNum );
  222.         
  223.     inPath = ShellGetVar( ShellWh, "PATH" );
  224.     
  225.     if( inPath )
  226.         return( OpenFileInPath( inPath, filePath, fileType, perm ) );
  227. }
  228.  
  229.  
  230. /*******************************************************************/
  231.  
  232. int16        CreateFile( char *filePath, int32 fileType )
  233. {
  234. int16        pwdVRefNum = -1, err;
  235. char        buf[ 64 ], path[ BUFSIZE ], *file;
  236. int32        parDirID, pwdDirID;
  237. pathType    pt;
  238. FInfo        finder;
  239.  
  240.     strcpy( path, filePath );
  241.     pt = SetCurrPath( path );
  242.     
  243.     if( pt == pathIsFile )
  244.         strcpy( buf, GetLastScan() );
  245.     else
  246.         {
  247.         file = ExtractFile( path );
  248.         strcpy( buf, file );    /* we now have the file name */
  249.         
  250.         if( file != path )    /* directory path found  */
  251.             {
  252.             file--;            /* set path */
  253.             *file = '\0';
  254.             pt = SetCurrPath( path );    /* look for dir */
  255.             }
  256.         }
  257.         
  258.     GetPWDInfo( &pwdVRefNum, &pwdDirID, &parDirID );
  259.     CtoPstr( buf );
  260.  
  261.     if( pt == pathIsFile )    /* file exists delete it */
  262.         {
  263.         err = HGetFInfo( pwdVRefNum, pwdDirID, buf, &finder );
  264.         
  265.         if( !err )
  266.             {
  267.             if( finder.fdType == fileType )
  268.                 err = HDelete( pwdVRefNum, pwdDirID, buf );
  269.     
  270.             err = HCreate( pwdVRefNum, pwdDirID, buf, ShellPrefs.TextCreator, 'TEXT' );
  271.             }
  272.         }
  273.     else
  274.         err = HCreate( pwdVRefNum, pwdDirID, buf, ShellPrefs.TextCreator, 'TEXT' );
  275.     
  276.     return( err );
  277. }
  278.  
  279. /*******************************************************************/
  280.  
  281. OSErr    RmTree( char *name, int16 vRefNum, int32 parDirID )
  282. {
  283. OSErr        err;
  284. int16        entries;
  285. int32         dirID;
  286. CInfoPBRec    scanPB;
  287. char        scan[ 256 ];
  288.  
  289. /* get number of entries */
  290.     strcpy( scan, name );
  291.     CtoPstr( scan );
  292.  
  293.     scanPB.dirInfo.ioFDirIndex     = 0;        /* name in this directory */
  294.     scanPB.dirInfo.ioCompletion = 0L; 
  295.     scanPB.dirInfo.ioNamePtr    = (StringPtr) scan;
  296.     scanPB.dirInfo.ioVRefNum     = vRefNum;
  297.     scanPB.dirInfo.ioDrDirID    = parDirID;    /* parent specified */
  298.     
  299.     err = PBGetCatInfo( &scanPB, FALSE );
  300.     if( err )
  301.         return( err );
  302.     
  303.     entries = scanPB.dirInfo.ioDrNmFls + 1;
  304.     dirID = scanPB.dirInfo.ioDrDirID;
  305.     
  306. /*
  307.  *    for each entry
  308.  *        if a file then
  309.  *            delete file
  310.  *        else if a directory then
  311.  *            ForceRmDir
  312.  */
  313.      while( --entries )
  314.         {
  315.         CursorWait();
  316.         if( abortFileOp || UserAbort() )
  317.             {
  318.             abortFileOp = TRUE;
  319.             break;
  320.             }
  321.             
  322.         scanPB.hFileInfo.ioFDirIndex     = entries;
  323.         scanPB.hFileInfo.ioCompletion     = 0L; 
  324.         scanPB.hFileInfo.ioNamePtr        = (StringPtr) scan;
  325.         scanPB.hFileInfo.ioVRefNum         = vRefNum;
  326.         scanPB.hFileInfo.ioDirID        = dirID;
  327.         
  328.         err = PBGetCatInfo( &scanPB, FALSE );
  329.  
  330.          if( err )
  331.             break;
  332.             
  333.         if( scanPB.hFileInfo.ioFlAttrib & 0x10 )    /* was a directory */
  334.             {
  335.             if( DirIsEmpty( scan, vRefNum, dirID ) )
  336.                 err = HDelete( vRefNum, dirID, scan );
  337.             else        
  338.                 {
  339.                 PtoCstr( scan );
  340.                  err = RmTree( scan, vRefNum, dirID );
  341.                  }
  342.              }
  343.         else                                        /* was a file */
  344.             err = HDelete( vRefNum, dirID, scan );
  345.  
  346.          if( err )
  347.             break;
  348.         }
  349.  
  350.     if( !err )
  351.         {
  352.         strcpy( scan, name );
  353.         CtoPstr( scan );
  354.         err = HDelete( vRefNum, parDirID, scan );
  355.         }
  356.         
  357.     SetArrow();
  358.     return( err );
  359. }
  360.  
  361. /*******************************************************************/
  362.  
  363. OSErr        CopyFork( srcRefNum, dstRefNum )
  364. {
  365. int16            err;
  366. char        buf[ BUFSIZE ];
  367. int32        bytesToRead,
  368.             fileLength;
  369.  
  370.     err = noErr;
  371.     bytesToRead = BUFSIZE;
  372.     err = GetEOF( srcRefNum, &fileLength );
  373.  
  374.     if( fileLength < bytesToRead )
  375.         bytesToRead = fileLength;
  376.  
  377.     while( bytesToRead )
  378.         {
  379.         err = FSRead( srcRefNum, &bytesToRead, buf );
  380.  
  381.         if( err )
  382.             break;
  383.  
  384.         err = FSWrite( dstRefNum, &bytesToRead, buf );
  385.  
  386.         if( err )
  387.             break;
  388.         
  389.         fileLength -= bytesToRead;
  390.         if( fileLength < bytesToRead )
  391.             bytesToRead = fileLength;
  392.         if( bytesToRead < 0 )
  393.             bytesToRead = 0;
  394.         }
  395. }
  396.  
  397. /*******************************************************************/
  398.  
  399. OSErr    CopyFile( char *srcName, char *dstName,
  400.                 int16 srcVRefNum, int16 dstVRefNum,
  401.                 int32 srcDirID, int32 dstDirID )
  402. {
  403. FInfo        finderInfo;
  404. int16        err;
  405. int16        srcRefNum = 0,
  406.             dstRefNum = 0;
  407. int32        bytesToRead,
  408.             fileLength;
  409.  
  410.     CtoPstr( srcName );
  411.     if( dstName != srcName )
  412.         CtoPstr( dstName );
  413.  
  414.     err = HGetFInfo( srcVRefNum, srcDirID, srcName, &finderInfo );
  415.     
  416.     err = HOpen( srcVRefNum, srcDirID, srcName, fsRdPerm, &srcRefNum );
  417.     if( err )    return( err );
  418.     
  419.     err = HDelete( dstVRefNum, dstDirID, dstName );
  420.     
  421.     err = HCreate( dstVRefNum, dstDirID, dstName, 
  422.                 finderInfo.fdCreator, finderInfo.fdType );
  423.     if( err )    return( err );
  424.  
  425.     err = HOpen( dstVRefNum, dstDirID, dstName, fsWrPerm, &dstRefNum );
  426.  
  427.     if( err )
  428.         {
  429.         FSClose( srcRefNum );
  430.         return( err );
  431.         }
  432.  
  433.     err = CopyFork( srcRefNum, dstRefNum );
  434.     
  435.     err = FSClose( srcRefNum );
  436.     err = FSClose( dstRefNum );
  437.  
  438.     err = HOpenRF( srcVRefNum, srcDirID, srcName, fsRdPerm, &srcRefNum );
  439.     if( err )    return( err );
  440.     
  441.     err = HOpenRF( dstVRefNum, dstDirID, dstName, fsWrPerm, &dstRefNum );
  442.     if( err )
  443.         {
  444.         FSClose( srcRefNum );
  445.         return( err );
  446.         }
  447.  
  448.     err = CopyFork( srcRefNum, dstRefNum );
  449.  
  450.     err = FSClose( srcRefNum );
  451.     err = FSClose( dstRefNum );
  452.     
  453.     return( err );
  454. }
  455.  
  456. /*******************************************************************/
  457.  
  458. OSErr        CopyDir( char *srcName, char *dstName,
  459.                 int16 srcVRefNum, int16 dstVRefNum,
  460.                 int32 srcParDirID, int32 dstDirID )
  461. {
  462. OSErr        err;
  463. int32         newDirID, srcDirID;
  464. int16        entries;
  465. CInfoPBRec    scanPB;
  466. char        scan[ 256 ];
  467.  
  468. /* create new directory */
  469.  
  470.     CtoPstr( dstName );
  471.     err = DirCreate( dstVRefNum, dstDirID, dstName, &newDirID );
  472.  
  473.     if( err )
  474.         return( err );
  475.  
  476. /* get number of entries */
  477.  
  478.     CtoPstr( srcName );
  479.     scanPB.dirInfo.ioFDirIndex     = 0;    /* this directory */
  480.     scanPB.dirInfo.ioCompletion = 0L; 
  481.     scanPB.dirInfo.ioNamePtr    = (StringPtr) srcName;
  482.     scanPB.dirInfo.ioVRefNum     = srcVRefNum;
  483.     scanPB.dirInfo.ioDrDirID    = srcParDirID;
  484.     
  485.     err = PBGetCatInfo( &scanPB, FALSE );
  486.  
  487.     if( err )
  488.         return( err );
  489.     
  490.     srcDirID = scanPB.dirInfo.ioDrDirID;
  491.     entries = scanPB.dirInfo.ioDrNmFls + 1;
  492.  
  493. /*
  494.  *    for each entry
  495.  *        if a file then
  496.  *            copy file
  497.  *        else if a directory then
  498.  *            copy dir
  499.  */
  500.      while( entries-- )
  501.         {
  502.         CursorWait();
  503.         if( abortFileOp || UserAbort() )
  504.             {
  505.             abortFileOp = TRUE;
  506.             break;
  507.             }
  508.             
  509.         scanPB.hFileInfo.ioFDirIndex     = entries;
  510.         scanPB.hFileInfo.ioCompletion     = 0L; 
  511.         scanPB.hFileInfo.ioNamePtr        = (StringPtr) scan;
  512.         scanPB.hFileInfo.ioVRefNum         = srcVRefNum;
  513.         scanPB.hFileInfo.ioDirID        = srcDirID;
  514.         
  515.         err = PBGetCatInfo( &scanPB, FALSE );
  516.  
  517.          if( err )
  518.             break;
  519.             
  520.         PtoCstr( scan );
  521.         if( scanPB.hFileInfo.ioFlAttrib & 0x10 )    /* was a directory */
  522.              err = CopyDir( scan, scan, srcVRefNum, dstVRefNum, srcDirID, newDirID );
  523.         else
  524.             err = CopyFile( scan, scan, srcVRefNum, dstVRefNum, srcDirID, newDirID );
  525.  
  526.          if( err )
  527.             break;
  528.         }
  529.  
  530.     SetArrow();
  531.     return( err );
  532. }
  533.  
  534. /*******************************************************************/
  535.  
  536. OSErr        MoveFile( char *srcName, char *dstName,
  537.                 int16 srcVRefNum, int16 dstVRefNum,
  538.                 int32 srcDirID, int32 dstDirID )
  539. {
  540. OSErr  err;
  541.  
  542.     if( srcDirID == dstDirID )    /* rename ? */
  543.         {
  544.         if( strcmp( srcName, dstName ) == 0 )    /* ? */
  545.             return( noErr );
  546.             
  547.         CtoPstr( srcName );
  548.         CtoPstr( dstName );
  549.         err = HRename( srcVRefNum, srcDirID, srcName, dstName );
  550.         }
  551.     else if( srcVRefNum != dstVRefNum )    /* from one volume to another */
  552.         {
  553.         err = CopyFile( srcName, dstName, srcVRefNum, dstVRefNum, srcDirID, dstDirID );
  554.         if( !err )
  555.             err = HDelete( srcVRefNum, srcDirID, srcName );
  556.         }
  557.     else    /* its a move */
  558.         {
  559.         CtoPstr( srcName );
  560.         
  561.         err = GetDirName( dstVRefNum, dstDirID, dstName );
  562.         err = GetParID( dstVRefNum, dstDirID, &dstDirID );
  563.         err = CatMove( srcVRefNum, srcDirID, srcName, dstDirID, dstName );
  564.         }
  565.         
  566.     return( err );
  567. }
  568.  
  569. /*******************************************************************/
  570.  
  571. OSErr        MoveDir( char *srcName, char *dstName,
  572.                 int16 srcVRefNum, int16 dstVRefNum,
  573.                 int32 srcDirID, int32 dstDirID )
  574. {
  575. OSErr  err;
  576.  
  577.     if( srcDirID == dstDirID )    /* rename ? */
  578.         {
  579.         if( strcmp( srcName, dstName ) == 0 )    /* ? */
  580.             return( noErr );
  581.             
  582.         CtoPstr( srcName );
  583.         CtoPstr( dstName );
  584.         err = HRename( srcVRefNum, srcDirID, srcName, dstName );
  585.         }
  586.     else if( srcVRefNum != dstVRefNum )    /* from one volume to another */
  587.         {
  588.         err = CopyDir( srcName, dstName, srcVRefNum, dstVRefNum, srcDirID, dstDirID );
  589.         if( !err )
  590.             err = RmTree( srcName, srcVRefNum, srcDirID );
  591.         }
  592.     else    /* its a move */
  593.         {
  594.         CtoPstr( srcName );
  595.         
  596.         err = GetDirName( dstVRefNum, dstDirID, dstName );
  597.         err = GetParID( dstVRefNum, dstDirID, &dstDirID );
  598.         err = CatMove( srcVRefNum, srcDirID, srcName, dstDirID, dstName );
  599.         }
  600.         
  601.     return( err );
  602. }